package com.lancoo.ccas52.redis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lancoo.ccas52.entity.TeachingClassStudent;
import com.lancoo.ccas52.mapper.TeachingClassStudentMapper;
import com.lancoo.ccas52.pojo.common.StudentCourse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author： Lining
 * @date： 2021/8/16 16:22
 * @description： redis发送订阅模式消费者，监听订阅消息
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
@Component
public class RedisMQSubscriber implements MessageListener {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;


    @Resource
    private TeachingClassStudentMapper teachingClassStudentMapper;

//    private final Queue<Message> messageQueue = new LinkedBlockingQueue<>();
//
//    @Override
//    public void onMessage(Message message, byte[] pattern) {
//        messageQueue.add(message);
//    }
//
//    @PostConstruct
//    public void init() {
//        // 单独的线程处理消息队列中的消息
//        new Thread(() -> {
//            while (true) {
//                if (!messageQueue.isEmpty()) {
//                    Message message = messageQueue.poll();
//                    try {
//                        handleMessage(message);
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                } else {
//                    try {
//                        // 休眠一段时间
//                        Thread.sleep(100);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
//    }
//
//    @SneakyThrows
//    private void handleMessage(Message message) {
//        String topic = new String(message.getChannel());
//        byte[] body = message.getBody();
//        // 学生添加预选消息消费
//        if ("student_add_formal_course".equals(topic)) {
//            TeachingClassStudent teachingClassStudent = (TeachingClassStudent) redisTemplate.getValueSerializer().deserialize(body);
//            ArrayList<TeachingClassStudent> teachingClassStudents = ListUtil.toList(teachingClassStudent);
//            teachingClassStudentMapper.addBatch(teachingClassStudents);
//            log.info("学生添加消息={}消费成功", teachingClassStudent);
//        }
//        if ("student_drop_formal_course".equals(topic)) {
//            TeachingClassStudent teachingClassStudent = (TeachingClassStudent) redisTemplate.getValueSerializer().deserialize(body);
//            teachingClassStudentMapper.update(teachingClassStudent, new LambdaUpdateWrapper<TeachingClassStudent>()
//                    .eq(TeachingClassStudent::getTeachingClassId, teachingClassStudent.getTeachingClassId())
//                    .eq(TeachingClassStudent::getStudentId, teachingClassStudent.getStudentId())
//                    .set(TeachingClassStudent::getFlag, teachingClassStudent.getFlag()));
//            log.info("学生退选消息={}消费成功", teachingClassStudent);
//        }
//    }
//}

    @SneakyThrows
    @Override
    public void onMessage(Message message, byte[] pattern) {
//        Thread.sleep(10);
        //根据订阅主题消费
        String topic = new String(pattern);
        //学生添加预选消息消费
        if ("student_add_formal_course".equals(topic)) {
            Thread.sleep(10);
            TeachingClassStudent teachingClassStudent = (TeachingClassStudent) redisTemplate.getValueSerializer().deserialize(message.getBody());
            ArrayList<TeachingClassStudent> teachingClassStudents = ListUtil.toList(teachingClassStudent);
            teachingClassStudentMapper.addBatch(teachingClassStudents);
            log.info("学生添加消息={}消费成功", teachingClassStudent);
        }
        if ("student_drop_formal_course".equals(topic)) {
            Thread.sleep(10);
            TeachingClassStudent teachingClassStudent = (TeachingClassStudent) redisTemplate.getValueSerializer().deserialize(message.getBody());
            teachingClassStudentMapper.update(teachingClassStudent, new LambdaUpdateWrapper<TeachingClassStudent>()
                    .eq(TeachingClassStudent::getTeachingClassId, teachingClassStudent.getTeachingClassId())
                    .eq(TeachingClassStudent::getStudentId, teachingClassStudent.getStudentId())
                    .set(TeachingClassStudent::getFlag, teachingClassStudent.getFlag()));
            log.info("学生退选消息={}消费成功", teachingClassStudent);
        }
    }
}

